﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace peonwar
{
    public class Chat
    {
        public bool Enabled;
        public List<string> Chatlist;
        public List<Color> Colorlist;
        public KbHandler input;
        private int delay;
        private bool Fading;
        private Vector2 Position;
        private int Capacity;
        private Color WriteColor;
        public Color PlayerColor;
        private int BaseYPos;
        private bool LobbyChat;
        private SpriteFont Font;

        public Chat(SpriteFont font, Vector2 position, int capacity, Color writecolor, Color playercolor, bool lobbychat, int kbtaille, string playerpseudo)
        {
            this.Font = font;
            this.WriteColor = writecolor;
            this.PlayerColor = playercolor;
            this.Capacity = capacity;
            this.Position = position;
            this.BaseYPos = (int)position.Y - ((20 * Capacity) + 10);
            this.LobbyChat = lobbychat;
            this.input = new KbHandler(Font, WriteColor, Position, kbtaille, playerpseudo);
        }

        public void Initialize()
        {
            Chatlist = new List<string> { };
            Colorlist = new List<Color> { };
            Enabled = false;
            delay = 1;
            
            Fading = false;
            if (LobbyChat)
            {
                for (int i = 0; i < Capacity; i++)
                {
                    Chatlist.Add("");
                    Colorlist.Add(Color.White);
                }
                Enabled = true;
                Capacity = Capacity - 1;
            }
        }

        public void Toggle()
        {
            Enabled = !Enabled;
            input.Reset();
        }

        public string Update(int highticks, KeyboardState kb)
        {
            delay++;
            if (delay > SettingManager.CurrentProfil.Chatdelay * 60)
            {
                delay = 1;
                if (Chatlist.Count() > 0 && !LobbyChat)
                {
                    User.AdvertList.Add(new Advert(new Vector2(Position.X, BaseYPos + 5), input.Font, Chatlist[0], 0.3f, Vector2.Zero, 0f, Colorlist[0], false, false));
                    Chatlist.RemoveAt(0);
                    Colorlist.RemoveAt(0);
                    Fading = true;
                }
            }

            if (Fading && delay > 30)
                Fading = false;

            if (Enabled && !DevConsole.Enabled)
            {
                Random tho = new Random();
                int wait = tho.Next(1, 101);
                input.Update(highticks);
                if (kb.IsKeyDown(Keys.Enter))
                {
                    
                    if (input.stock != "")
                    {
                        if (LobbyChat)
                            EnterChat(SettingManager.CurrentProfil.Name + ": " + input.stock, PlayerColor);
                        else
                        {
                            if (input.stock == "!rtd")
                                EnterChat(SettingManager.CurrentProfil.Name + " has rolled the dice : " + wait, PlayerColor);
                            else if (input.stock == "!gay")
                                EnterChat("coucou", Color.Pink);
                            else EnterChat(input.stock, PlayerColor);


                            Enabled = false;
                            
                        }
                        string result = input.stock;
                        input.Reset();

                        return result;
                    }
                }
            }
            return "";
        }

        public void EnterChat(string message, Color color)
        {
            if (Chatlist == null)
                return;
            Chatlist.Add(message);
            Colorlist.Add(color);
            if (Chatlist.Count() == 1)
                delay = 1;
            else
                if (Chatlist.Count > Capacity)
                {
                    User.AdvertList.Add(new Advert(new Vector2(Position.X, BaseYPos + 5), input.Font, Chatlist[0], 0.3f, Vector2.Zero, 0f, Colorlist[0], false, false));
                    Chatlist.RemoveAt(0);
                    Colorlist.RemoveAt(0);
                    Fading = true;
                    delay = 1;
                }
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            int offset = Fading ? 20 - (int)(((20f / 30f) * (float)delay)) : 0;
            for (int i = 0; i < Chatlist.Count(); i++)
            {
                spriteBatch.DrawString(input.Font, Chatlist[i], new Vector2(Position.X, BaseYPos + 5 + (20 * i) + offset), Colorlist[i]);
            }

            if (Enabled)
            {
                if (!LobbyChat)
                    spriteBatch.Draw(GameTexture.Chat_, new Rectangle((int)Position.X - 65, -50 + BaseYPos, 300, 300), Color.White);
                input.DrawStock(spriteBatch);
            }
        }
    }
}
